package com.qfedu.web;

import com.qfedu.pojo.*;
import com.qfedu.service.AccountService;
import com.qfedu.service.LoanService;
import com.qfedu.service.ProductService;
import com.qfedu.service.UserService;
import com.qfedu.service.impl.AccountServiceImpl;
import com.qfedu.service.impl.LoanServiceImpl;
import com.qfedu.service.impl.ProductServiceImpl;
import com.qfedu.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@WebServlet("/loanServlet")
public class LoanServlet extends BaseServlet{
    LoanService loanService=new LoanServiceImpl();
    private ProductService productService = new ProductServiceImpl();
    private AccountService accountService = new AccountServiceImpl();
    private UserService userService = new UserServiceImpl();
    public void findAllLoan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Loan> loans = loanService.findAllLoan();
        request.getSession().setAttribute("loans",loans);
        response.sendRedirect("/pages/loan/loanProducts.jsp");
    }

    public void findLoanById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String where = request.getParameter("where");
        if (where.equals("1")){
            String id = request.getParameter("pid");
            int pid = Integer.parseInt(id);
            Loan loan = loanService.findLoanById(pid);
            if (loan!=null){
                request.getSession().setAttribute("MyLoan",loan);
                request.getRequestDispatcher("/pages/loan/myLoanDetail.jsp").forward(request,response);
            }else {
                //System.out.println("没有当前产品");
                request.getSession().setAttribute("msg","没有当前产品");
                request.getRequestDispatcher("/pages/loan/loanProducts.jsp").forward(request,response);
            }
        }
    }
    public void loan(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        User user = (User) request.getSession().getAttribute("user");//点击了我的产品，先判断登陆对象是否为空
        if (user != null) {
            String phone = user.getPhone();
            Account account = accountService.queryIdcodeByPhone(phone);
            String idcode = account.getIdcode();
            if (idcode != null) {

                Account accountByIdcode = accountService.queryAccountByIdcode(idcode);
                request.getSession().setAttribute("MyAccount",accountByIdcode);
                Loan myLoan = (Loan)request.getSession().getAttribute("MyLoan");

                response.sendRedirect("/pages/loan/payLoan.jsp");
            }
        }else {
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request,response);
        }
    }
    public void isLoan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Account myAccount = (Account)request.getSession().getAttribute("MyAccount");
        String password = request.getParameter("password");
        String paypassword = myAccount.getPaypassword();
        if (!paypassword.equals(password)){
            request.getSession().setAttribute("msg","当前支付密码不正确");
            request.getRequestDispatcher("/pages/loan/payLoan.jsp").forward(request,response);
        }else {
            //找到当前商品的单价
            Loan myLoan = (Loan)request.getSession().getAttribute("MyLoan");
            int pid=myLoan.getPid();

            BigDecimal price = new BigDecimal(request.getParameter("pnum"));
            User user = (User)request.getSession().getAttribute("user");
            Account account = accountService.queryIdcodeByPhone(user.getPhone());
            BigDecimal money = account.getMoney();
            BigDecimal add = price.add(money);
            int i = accountService.updateMoney(account , add);
            String s= request.getParameter("loan_f");
            int loan_f=Integer.parseInt(s);
            int back=Integer.parseInt(request.getParameter("back"));
            String idcode = myAccount.getIdcode();
            String pname=request.getParameter("pname");
            loanService.addLoan(idcode,pid,price, back,pname, loan_f);

            //request.getRequestDispatcher("/pages/product/paysuccess.jsp").forward(request,response);
            request.getRequestDispatcher("/pages/loan/loansuccess.jsp").forward(request,response);

        }
    }
    public void findMyLoan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute("user");//点击了我的产品，先判断登陆对象是否为空
        if (user!=null){
            String phone = user.getPhone();
            Account account = accountService.queryIdcodeByPhone(phone);
            String idcode = account.getIdcode();
            List<MyLoan> loanList = loanService.findMyLoan(idcode);

                request.getSession().setAttribute("loansList",loanList);
                request.getRequestDispatcher("/pages/loan/myloan.jsp").forward(request,response);


        }else {
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request,response);
        }

    }



















//
//    public void back(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//        User user = (User) request.getSession().getAttribute("user");//点击了我的产品，先判断登陆对象是否为空
//        if (user != null) {
//            String phone = user.getPhone();
//            Account account = accountService.queryIdcodeByPhone(phone);
//            String idcode = account.getIdcode();
//            if (idcode != null) {
//                //找到当前电子余额
//                ProductService productService = new ProductServiceImpl();
//                Account accountByIdcode = accountService.queryAccountByIdcode(idcode);
//                BigDecimal leftMoney = accountByIdcode.getMoney();
//                request.getSession().setAttribute("myAccount",accountByIdcode);
//                MyProduct myProduct = (MyProduct)request.getSession().getAttribute("myProduct");
//                System.out.println(myProduct);
//                response.sendRedirect("/pages/product/backproduct.jsp");
//            }
//        }else {
//            request.getRequestDispatcher("/pages/user/login.jsp").forward(request,response);
//        }
//    }

}
